package com.shop.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.DtsCommentMapper;
import com.shop.pojo.DtsComment;
import com.shop.service.DtsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DtsCommentServiceImpl
 * @Author xiao宋
 * @Date 10:15  2022/10/22
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsCommentServiceImpl implements DtsCommentService {
    @Autowired
    DtsCommentMapper commentMapper;

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, Integer goodsId, Integer userId, List<Integer> brandIds) {
        IPage<DtsComment> iPage = new Page<>(page, limit);
        QueryWrapper<DtsComment> w = new QueryWrapper<>();
//        排序
        if ("desc".equalsIgnoreCase(order)) {
            w.orderByDesc("c." + sort);
        } else {
            w.orderByAsc("c." + sort);
        }
//        用户 ID
        if (userId != null) {
            w.orderByDesc("c.user_id" + userId);
        }
//        商品 ID
        if (goodsId != null) {
            w.orderByAsc("c.value_id" + goodsId);
        }
//        品牌 ID
        if (brandIds != null && brandIds.size() > 0) {
            w.orderByAsc("g.brand_id" + brandIds);
        }
        commentMapper.selectCommentAndBrandPage(iPage, w);
        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());

        return data;
    }

    @Override
    public void delete(Integer id) {
        commentMapper.deleteById(id);
    }
}
